package com.squareup.picasso;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

final class Stats {
    private static final int REQUESTED_COMPLETED = 0;
    private static final int CACHE_HIT = 1;
    public static final int CACHE_MISS = 2;
    private static final int BITMAP_DECODE_FINISHED = 3;
    private static final int BITMAP_TRANSFORMED_FINISHED = 4;

    private static final String STATS_THREAD_NAME = Utils.THREAD_PREFIX + "Stats";

    final class StatsHandler extends Handler {
        private final Stats stats;

        public StatsHandler(Looper looper, Stats stats) {
            super(looper);
            this.stats = stats;
        }

        public final void handleMessage(final Message message) {
            long v2;
            Stats v0;
            switch (message.what) {
                case 0: {
                    this.stats.c += 1;
                    break;
                }
                case 1: {
                    this.stats.cacheHits += 1;
                    break;
                }
                case 2: {
                    v2 = ((long) message.arg1);
                    ++stats.cacheMisses;
                    stats.f = v2 + stats.f;
                    stats.i = stats.f / (((long) stats.cacheMisses));
                    break;
                }
                case 3: {
                    long size2 = ((long) message.arg1);
                    ++stats.originalBitmapCount;
                    stats.totalOriginalBitmapSize = size2 + stats.totalOriginalBitmapSize;
                    stats.averageOriginalBitmapSize = stats.totalOriginalBitmapSize / (long) stats.originalBitmapCount;
                    break;
                }
                case 4: {
                    long size = (long) message.obj;
                    ++stats.transformedBitmapCount;
                    stats.totalTransformedBitmapSize += size;
                    stats.averageTransformedBitmapSize = stats.totalTransformedBitmapSize / (((long) stats.transformedBitmapCount));
                    break;
                }
                default: {
                    Picasso.dispatcherHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            throw new AssertionError("Unhandled stats message." + message.what);
                        }
                    });
                    break;
                }
            }
        }
    }

    final Cache cache;
    final Handler handler;
    long c;
    long cacheHits;
    long totalTransformedBitmapSize;
    long f;
    long totalOriginalBitmapSize;
    long averageTransformedBitmapSize;
    long i;
    long averageOriginalBitmapSize;
    int transformedBitmapCount;
    int cacheMisses;
    int originalBitmapCount;
    private HandlerThread statsThread;

    Stats(Cache cache) {
        this.cache = cache;
        this.statsThread = new HandlerThread("Picasso-Stats", 10);
        this.statsThread.start();
        Utils.flushStackLocalLeaks(this.statsThread.getLooper());
        this.handler = new StatsHandler(this.statsThread.getLooper(), this);
    }

    final void dispatchCacheHit() {
        this.handler.sendEmptyMessage(0);
    }

    final void processBitmap(Bitmap bitmap, int what) {
        this.handler.sendMessage(this.handler.obtainMessage(what, Utils.getBitmapBytes(bitmap), 0));
    }
}

